package com.bilibili.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilibili.entity.CommonResult;
import com.bilibili.entity.Page;
import com.bilibili.entity.Video;
import com.bilibili.service.CommentService;
import com.bilibili.service.DanmakuService;
import com.bilibili.service.VideoService;
import com.bilibili.service.impl.CommentServiceImpl;
import com.bilibili.service.impl.DanmakuServiceImpl;
import com.bilibili.service.impl.VideoServiceImpl;

@SuppressWarnings("serial")
@WebServlet("/backVideoQueryAllInfo")
public class BackVideoQueryServlet extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("application/json; charset=utf-8");
		int pageNum = 1,pageSize = 5;
		if(request.getParameter("pageNum") != null){
			pageNum = Integer.parseInt(request.getParameter("pageNum"));
		}
		
		if(request.getParameter("pageSize") != null){
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		Video v = new Video();
		String title = request.getParameter("title");
		String state = request.getParameter("state");
		int status = -1;
		if(state != null && !"".equals(state)) {
			if(state.equals("已发布")) {
				status = 1;
			}else if(state.equals("待审核")) {
				status = 0;
			}else if(state.equals("已下架")) {
				status = 2;
			}else {
				status = -1;
			}
		}
		v.setTitle(title);
		v.setStatus(status);
		VideoService vs = new VideoServiceImpl();
		CommentService cs = new CommentServiceImpl();
		DanmakuService ds = new DanmakuServiceImpl();
		Page<Video> videoPage = vs.selectAllVideo(v, pageNum, pageSize);
		List<Object> allInfo = new ArrayList<Object>();
		allInfo.add(videoPage);
		allInfo.add(cs.selectAllCommentNum());
		allInfo.add(ds.selectAllDanmakuNum());
		allInfo.add(vs.selectNotExamineVideoNum());
		allInfo.add(vs.selectAllVideoNum());
		CommonResult cr = new CommonResult();
		if(videoPage.getData().size() != 0) {
			cr.setCode(200);
			cr.setData(allInfo);
		}else {
			cr.setCode(409);
		}
		response.getWriter().write(cr.toJSONString());
	}
}
